package com.example.demo.controller;

import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.word.WordExportUtil;
import com.example.demo.serivce.Impl.IndexService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/test")
public class wordController {

    @Autowired
    IndexService indexService;

    @GetMapping(value = "/down")
    public void test(HttpServletResponse response) throws Exception{
        File rootFile =new File(ResourceUtils.getURL("classpath:").getPath());
        File templateFile= new File(rootFile,"/word/客流分析报告-world模板.docx");
        Map<String,Object> resultListParams =new HashMap<>();
        resultListParams.put("region_name","延安路");
        ImageEntity image = new ImageEntity("static/image/123.jpg", 200, 300);
        image.setType(ImageEntity.URL);
        resultListParams.put("image", image);
        XWPFDocument word = WordExportUtil.exportWord07(templateFile.getPath(),resultListParams);
        String filename = "测试demo.docx";


        response.setHeader("content-disposition","attachment;filename="+new String(filename.getBytes(),"ISO8859-1"));
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        word.write(response.getOutputStream());

    }

    @PostMapping("/getUri")
    public void getUri(){
        indexService.getUri();
    }

    public static void main(String[] args) {
        Integer i = 1111;
    }
}
